package rmhc.gui
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	import rmhc.RMHCApp;
	import rmhc.api.dataflow.UserInfo;
	import rmhc.design.FriendCellDesign;
	
	public class FriendCell extends FriendCellDesign
	{
		protected var info:UserInfo;
		protected var used:Boolean = false;
		
		public var thumb:Loader;
		
		public function FriendCell( info:UserInfo )
		{
			super();
			this.info = info;
			
			setTitles( info.firstname , info.lastname );
			
			thumb = new Loader();
			//addImg( thumb );
			
			addEventListener( MouseEvent.MOUSE_OVER , onOver );
			addEventListener( MouseEvent.MOUSE_OUT , onOut );
		}
		
		public function init():void
		{
			//грузим картинку
			if ( info.thumbSrc == null )
			{
				onLoadThumb( null );
				return;
			}
			var req:URLRequest = new URLRequest( info.thumbSrc );
			thumb.contentLoaderInfo.addEventListener(Event.COMPLETE , onLoadThumb );
			thumb.load( req , RMHCApp.getApi().getContext() );
		}
		
		protected function onLoadThumb( e:Event ):void
		{
			if ( thumb.content != null )
			{
				thumb.alpha = 0;
				( thumb.content as Bitmap ).smoothing = true;
				addImg( thumb );
				TweenLite.to( thumb , 0.8 , { alpha : 1 } );
			}
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
		
		protected function onOver( e:MouseEvent ):void
		{
			if ( used )
			{
				return;
			}
			tween( 0 );
		}
		protected function onOut( e:MouseEvent ):void
		{
			if ( used )
			{
				return;
			}
			tween( whiteAlpha );
		}
		
		public function setUsed( act:Boolean ):void
		{
			this.used = act;
			mouseEnabled = !used;
			var a:Number = used ? whiteAlpha : 0;
			tween( a );
			TweenLite.to( tfTit , 0.5 , { alpha : ( used ? 0 : 1 ) } );
		}
		protected function tween( a:Number ):void
		{
			TweenLite.to( white , 0.5 , { alpha : a } );
		}
		
		public function getInfo():UserInfo
		{
			return info;
		}
	}
}